//
//  NSDictionary+FTJson.m
//  FashionTech
//
//  Created by JackieYip on 2016/10/20.
//  Copyright © 2016年 mondial-d. All rights reserved.
//

#import "NSDictionary+FTJson.h"

@implementation NSDictionary (FTJson)
// jsonString -> jsonData -> jsonDictionary
+ (void)jsonDictionaryWithJsonString:(NSString *)jsonString onResponse:(void (^)(NSDictionary *jsonDictionary, NSError *error))onResponse
{
    if (onResponse) {
        if (jsonString.length < 1) {
            onResponse(nil, nil);
            return;
        }
        NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
        NSError *error = nil;
        NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
        if (error)
            onResponse(nil, error);
        else
            onResponse(jsonDictionary, nil);
    }
}
// jsonDictionary -> jsonData -> jsonString
+ (void)jsonStringWithJsonDictionary:(NSDictionary *)jsonDictionary onResponse:(void(^)(NSString *jsonString, NSError *error))onResponse
{
    if (onResponse) {
        if (jsonDictionary.count < 1) onResponse(nil, nil);
        NSError *error = nil;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:NSJSONWritingPrettyPrinted error:&error];
        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];;
        if (error)
            onResponse(nil, error);
        else
            onResponse(jsonString, nil);
    }
}
@end
